package viettravel.DTO;

import java.util.ArrayList;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONObject;
import android.graphics.drawable.Drawable;
import android.util.Log;
import viettravel.DAO.Comment;
import viettravel.common.Constants;

public class DBCommunication {

	public static String parserRegions(String root, String suffix) {
		JSONParser jsonParser = new JSONParser();
		JSONObject jsonRoot = jsonParser.getJSONFromUrl(root + suffix);
		String data = "";
		try {
			JSONArray jsonArray = jsonRoot.getJSONArray(Constants.TAG_REGIONS);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject item = jsonArray.getJSONObject(i);
				Integer id = item.getInt(Constants.TAG_REGION_ID);
				String name = item.getString(Constants.TAG_REGION_NAME);
				String description = item
						.getString(Constants.TAG_REGION_DESCRIPTION);
				data += name + " ";
				ViettravelDTO.getInstance().insertRegion(id, name, description);
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
		return data;
	}

	public static String parserProvinces(String root, String suffix) {
		JSONParser jsonParser = new JSONParser();
		JSONObject jsonRoot = jsonParser.getJSONFromUrl(root + suffix);
		String data = "";
		try {
			JSONArray jsonArray = jsonRoot
					.getJSONArray(Constants.TAG_PROVINCES);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject item = jsonArray.getJSONObject(i);
				Integer id = item.getInt(Constants.TAG_PROVINCE_ID);
				String name = item.getString(Constants.TAG_PROVINCE_NAME);
				String description = item
						.getString(Constants.TAG_PROVINCE_DESCRIPTION);
				Integer regionId = item.getInt(Constants.TAG_REGION_ID);
				data += name + " ";
				ViettravelDTO.getInstance().insertProvince(id, name,
						description, regionId);
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
		return data;
	}

	public static String parserPlaces(String root, String suffix) {
		JSONParser jsonParser = new JSONParser();
		JSONObject jsonRoot = jsonParser.getJSONFromUrl(root + suffix);
		Log.d(Constants.TAG, jsonRoot.toString());
		String data = "";
		try {
			JSONArray jsonArray = jsonRoot.getJSONArray(Constants.TAG_PLACES);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject item = jsonArray.getJSONObject(i);
				Log.d(Constants.TAG, item.toString());
				Integer id = item.getInt(Constants.TAG_PLACES_ID);
				String name = item.getString(Constants.TAG_PLACES_NAME);
				String description = item
						.getString(Constants.TAG_PLACES_DESCIPTION);
				Float rating = (float) item
						.getDouble(Constants.TAG_PLACES_RATING);
				/*
				 * Integer total_rate = item
				 * .getInt(Constants.TAG_PLACES_TOTAL_RATE);
				 */
				Float latitude = (float) item
						.getDouble(Constants.TAG_PLACES_LATITUDE);

				Float longtitude = (float) item
						.getDouble(Constants.TAG_PLACES_LONGTITUDE);
				Log.i(Constants.TAG + "insert", latitude + "," + longtitude);
				Integer provinceId = item.getInt(Constants.TAG_PROVINCE_ID);
				ViettravelDTO.getInstance().insertPlace(id, name, description,
						latitude, longtitude, rating, provinceId);
				JSONArray activityArray = item
						.getJSONArray(Constants.TAG_ACTIVITIES);
				for (int j = 0; j < activityArray.length(); j++) {
					Integer activityId = activityArray.getInt(j);
					ViettravelDTO.getInstance().insertPlace_Activity(id,
							activityId);
				}

				JSONArray tourArray = item
						.getJSONArray(Constants.TAG_TOURISTFORMS);
				Log.v(Constants.TAG + "tourArray", tourArray.length() + "");
				for (int j = 0; j < tourArray.length(); j++) {
					Integer tourId = tourArray.getInt(j);
					ViettravelDTO.getInstance().insertPlace_Tour(id, tourId);
				}

				JSONArray specialtiesArray = item
						.getJSONArray(Constants.TAG_SPECIALTIES);
				for (int j = 0; j < specialtiesArray.length(); j++) {
					Integer specId = specialtiesArray.getInt(j);
					ViettravelDTO.getInstance().insertPlace_Specialty(id,
							specId);
				}

				data += name + " ";

			}

		} catch (Exception e) {
			Log.d(Constants.TAG, e.toString());
			// TODO: handle exception
		}
		return data;
	}

	public static String parserTouristForms(String root, String suffix) {
		JSONParser jsonParser = new JSONParser();
		JSONObject jsonRoot = jsonParser.getJSONFromUrl(root + suffix);
		String data = "";
		try {
			JSONArray jsonArray = jsonRoot
					.getJSONArray(Constants.TAG_TOURISTFORMS);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject item = jsonArray.getJSONObject(i);
				Integer id = item.getInt(Constants.TAG_TOURISTFORMS_ID);
				String name = item.getString(Constants.TAG_TOURISTFORMS_NAME);
				String note = item.getString(Constants.TAG_TOURISTFORMS_NOTE);
				data += name + " ";
				ViettravelDTO.getInstance().insertTourist(id, name, note);
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
		return data;
	}

	public static String parserActivities(String root, String suffix) {
		JSONParser jsonParser = new JSONParser();
		JSONObject jsonRoot = jsonParser.getJSONFromUrl(root + suffix);
		String data = "";
		try {
			JSONArray jsonArray = jsonRoot
					.getJSONArray(Constants.TAG_ACTIVITIES);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject item = jsonArray.getJSONObject(i);
				Integer id = item.getInt(Constants.TAG_ACTIVITIES_ID);
				String name = item.getString(Constants.TAG_ACTIVITIES_NAME);
				String note = item.getString(Constants.TAG_ACTIVITIES_NOTE);
				data += name + " ";
				ViettravelDTO.getInstance().insertActivitiy(id, name, note);
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
		return data;
	}

	public static String parserSpecialties(String root, String suffix) {
		JSONParser jsonParser = new JSONParser();
		JSONObject jsonRoot = jsonParser.getJSONFromUrl(root + suffix);
		String data = "";
		try {
			JSONArray jsonArray = jsonRoot
					.getJSONArray(Constants.TAG_SPECIALTIES);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject item = jsonArray.getJSONObject(i);
				Integer id = item.getInt(Constants.TAG_SPECIALTIES_ID);
				String name = item.getString(Constants.TAG_SPECIALTIES_NAME);
				String note = item.getString(Constants.TAG_SPECIALTIES_NOTE);
				data += name + " ";
				ViettravelDTO.getInstance().insertSpecialty(id, name, note);
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
		return data;
	}

	public static ArrayList<Comment> parserComments(String root, String suffix,
			Integer placeId, Integer page) {
		JSONParser jsonParser = new JSONParser();
		JSONObject jsonRoot = jsonParser.getJSONFromUrl(root + suffix
				+ placeId.toString() + "/" + page.toString());
		ArrayList<Comment> listCmt = new ArrayList<Comment>();
		try {
			JSONArray jsonArray = jsonRoot.getJSONArray(Constants.TAG_COMMENTS);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject item = jsonArray.getJSONObject(i);
				Integer cmtId = item.getInt(Constants.TAG_COMMENTS_ID);
				String cmtTitlle = item.getString(Constants.TAG_COMMENTS_TITLE);
				String cmtContent = item
						.getString(Constants.TAG_COMMENTS_CONTENT);
				String cmtDate = item.getString(Constants.TAG_COMMENTS_DATE);
				String cmtSender = item
						.getString(Constants.TAG_COMMENTS_SENDER);
				String cmtImage = item.getString(Constants.TAG_COMMENTS_IMAGE);
				Drawable drawable;
				if (cmtImage != "") {
					byte[] image = Constants.string2ByteArray(cmtImage);
					drawable = Constants.byteArray2Drawable(image);
				} else {
					drawable = null;
				}

				Date date = new Date(cmtDate);
				Comment cmt = new Comment(cmtId, cmtTitlle, cmtContent,
						drawable, date, cmtSender);
				listCmt.add(cmt);
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
		return listCmt;
	}

	public static ArrayList<Comment> parserComments(String root, String suffix,
			Integer placeId) {
		JSONParser jsonParser = new JSONParser();
		JSONObject jsonRoot = jsonParser.getJSONFromUrl(root + suffix
				+ placeId.toString());
		// Log.d(Constants.TAG, jsonRoot.toString());
		ArrayList<Comment> listCmt = new ArrayList<Comment>();
		try {
			JSONArray jsonArray = jsonRoot.getJSONArray(Constants.TAG_COMMENTS);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject item = jsonArray.getJSONObject(i);
				// Log.d(Constants.TAG, item.toString());
				Integer cmtId = item.getInt(Constants.TAG_COMMENTS_ID);
				String cmtTitlle = item.getString(Constants.TAG_COMMENTS_TITLE);
				String cmtContent = item
						.getString(Constants.TAG_COMMENTS_CONTENT);
				String cmtDate = item.getString(Constants.TAG_COMMENTS_DATE);
				String cmtSender = item
						.getString(Constants.TAG_COMMENTS_SENDER);
				String cmtImage = item.getString(Constants.TAG_COMMENTS_IMAGE);
				Log.v(Constants.TAG + "test", cmtImage.length() + "");
				Drawable drawable;
				if (cmtImage.length() > 0) {
					byte[] image = Constants.string2ByteArray(cmtImage);
					drawable = Constants.byteArray2Drawable(image);
				} else {
					drawable = null;
				}

				Date date = new Date(cmtDate);
				Comment cmt = new Comment(cmtId, cmtTitlle, cmtContent,
						drawable, date, cmtSender);
				listCmt.add(cmt);
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
		return listCmt;
	}

	public static ArrayList<Drawable> parserImages(String root, String suffix,
			Integer placeId) {
		JSONParser jsonParser = new JSONParser();
		JSONObject jsonRoot = jsonParser.getJSONFromUrl(root + suffix
				+ placeId.toString());
		// Log.d(Constants.TAG, jsonRoot.toString());
		ArrayList<Drawable> listImage = new ArrayList<Drawable>();
		try {
			JSONArray jsonArray = jsonRoot.getJSONArray(Constants.TAG_IMAGES);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject item = jsonArray.getJSONObject(i);
				String cmtImage = item.getString(Constants.TAG_IMAGES_IMAGE);
				Drawable drawable;
				byte[] image = Constants.string2ByteArray(cmtImage);
				drawable = Constants.byteArray2Drawable(image);
				listImage.add(drawable);
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
		return listImage;
	}

	public static Boolean postData(String root, String suffix, JSONObject json) {
		JSONParser parser = new JSONParser();
		try {
			JSONObject jsonRoot = parser.postJSON(root + suffix, json);
			return jsonRoot.getBoolean(Constants.TAG_RESULT);
		} catch (Exception e) {
			return false;
		}
	}
}